﻿/**
 * @file filemodelcontroller.cpp
 * @author huangxb
 * @email 18228033207@163.com
 * @date 2024.04.04
 * @license MIT License
 * @brief 用于对文件model初始化.
 *
 * @htmlonly
 * <span style="font-weight: bold">History</span>
 * @endhtmlonly
 * Version|Auther|Date|Describe
 * ------|----|------|--------
 * V1.0|huangxb|2024.04.04|创建
 */

/* 自定义头文件 */
#include "filemodelcontroller.h"
#include "filemodel.h"
#include "fileitem.h"
/* 库头文件 */
#include <QFileInfo>
#include <QFileIconProvider>
#include <QDir>
#include <QDateTime>

void CFileModelController::Load(const QString &strPath)
{
    m_pFileModel->setColumnCount(EFMH_COLUMN_COUNT);

    QStringList listHeader;
    listHeader << tr("");
    listHeader << tr("");
    listHeader << tr("file name");
    listHeader << tr("last modify time");
    listHeader << tr("file type");
    listHeader << tr("file size");
    m_pFileModel->setHorizontalHeaderLabels(listHeader);
    m_pFileModel->setHeaderData(EFMH_COLUMN_FILE_NAME, Qt::Horizontal, Qt::AlignLeft, Qt::TextAlignmentRole);
    m_pFileModel->setHeaderData(EFMH_COLUMN_LAST_MODIFY_TIME, Qt::Horizontal, Qt::AlignLeft, Qt::TextAlignmentRole);
    m_pFileModel->setHeaderData(EFMH_COLUMN_FILE_SIZE, Qt::Horizontal, Qt::AlignLeft, Qt::TextAlignmentRole);
    m_pFileModel->setHeaderData(EFMH_COLUMN_FILE_TYPE, Qt::Horizontal, Qt::AlignLeft, Qt::TextAlignmentRole);

    QFileInfo fiFile(strPath);
    if(fiFile.isFile()) {
        DoLoadFile(fiFile);
        return;
    }

    QDir dir(strPath);
    auto listFile = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllEntries, QDir::DirsFirst);
    /* 暂不递归所有文件 */
    for(auto &fiFile : listFile)
    {
        DoLoadFile(fiFile);
    }
}

CFileModel *CFileModelController::GetFileModel() const
{
    return m_pFileModel;
}

CFileModelController::CFileModelController(QObject *parent)
    : QObject{parent}
    , m_pFileModel(new CFileModel(this))
    , m_pIconProvider(new QFileIconProvider())
{

}

CFileModelController::~CFileModelController()
{
    delete m_pIconProvider;
}

void CFileModelController::DoLoadFile(const QFileInfo &fiFile, CFileItem *pParentItem)
{
    QList<QStandardItem*> listColumn;
    listColumn << PrepareFileItem(fiFile, EFMH_COLUMN_ERR);
    listColumn << PrepareFileItem(fiFile, EFMH_COLUMN_CHECK);
    listColumn << PrepareFileItem(fiFile, EFMH_COLUMN_FILE_NAME);
    listColumn << PrepareFileItem(fiFile, EFMH_COLUMN_LAST_MODIFY_TIME);
    listColumn << PrepareFileItem(fiFile, EFMH_COLUMN_FILE_TYPE);
    listColumn << PrepareFileItem(fiFile, EFMH_COLUMN_FILE_SIZE);

    if(pParentItem) {
        /* 父节点不为空，增加到父节点上即可 */
        pParentItem->appendRow(listColumn);
    }
    else {
        /* 父节点为空，需要增加到model上 */
        m_pFileModel->appendRow(listColumn);
    }
}

void CFileModelController::DoLoadDir(const QDir &/*dir*/, CFileItem */*pParentItem*/)
{
    /* 未实现 */
    Q_ASSERT(false);
}

CFileItem *CFileModelController::PrepareFileItem(const QFileInfo &fiFile, int nColumn) const
{
    switch(EFileModelHeader(nColumn)){
    case EFMH_COLUMN_ERR:{
        CFileItem* pItem = new CFileItem();
        pItem->setIcon(QPixmap(":/resources/image/error.png"));

        return pItem;
    }
    case EFMH_COLUMN_CHECK:{
        CFileItem* pItem = new CFileItem();
        pItem->setCheckable(true);
        pItem->setCheckState(Qt::Unchecked);

        return pItem;
    }
    case EFMH_COLUMN_FILE_NAME:{
        CFileItem* pItem = new CFileItem();
        pItem->setText(fiFile.fileName());
        pItem->setTextAlignment(Qt::AlignLeft);
        /* 获取文件的icon */
        auto icon = m_pIconProvider->icon(fiFile);
        pItem->setIcon(icon);

        return pItem;
    }
    case EFMH_COLUMN_LAST_MODIFY_TIME:{
        CFileItem* pItem = new CFileItem();
        QDateTime datetime = fiFile.lastModified();
        auto strText = datetime.toString("yyyy/M/d hh:mm");
        pItem->setText(strText);
        pItem->setTextAlignment(Qt::AlignLeft);

        return pItem;
    }
    case EFMH_COLUMN_FILE_TYPE:{
        return new CFileItem;
    }
    case EFMH_COLUMN_FILE_SIZE:{
        CFileItem* pItem = new CFileItem();
        auto nSize = fiFile.size();/* 字节 */
        auto nSizeKb = nSize/1024;
        auto strSizeKb = QString("%1 KB").arg(nSizeKb);
        pItem->setText(strSizeKb);
        pItem->setTextAlignment(Qt::AlignLeft);

        return pItem;
    }
    default:
        Q_ASSERT(false);
        return nullptr;
    }
}
